﻿var map = null;
var x1 = null;
var x2 = null;
var y1 = null;
var y2 = null;
var priceFrom = 0;
var priceEnd = 0;
var roomNum = 0;
var acrFrom = 0;
var acrEnd = 0;
var strSource = null;
var communityID = null;
var totalPage = null;
var HouseType = 3;
var page_prev;
var page_next;
var page_count;
var page_size = 6;
var page_index = 1;


function logadMap(strSourceType) {

    if (GBrowserIsCompatible()) {
        strSource = strSourceType
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(31.949302, 118.759371), 10);  //上海 北纬 22.52分  东经121度  第三个参数是缩放比例
        map.setUIToDefault();
        mapsize = map.getSize();
        map.enableScrollWheelZoom();

        this.IntMapEvent();
        this.GetMapLngAndLat();
        this.ObtainCommunity();
        getMapAreaList();
    }

}



function GetMapLngAndLat() {
    /// <summary>获取经纬度</summary>
    var E = 100;
    var G = map.fromContainerPixelToLatLng(new GPoint(64, E / 2));
    var D = map.fromContainerPixelToLatLng(new GPoint(mapsize.width - E / 2, mapsize.height - E / 2));
    this.x1 = G.lng();
    this.x2 = D.lng();
    this.y1 = D.lat();
    this.y2 = G.lat();
}


function GetPhotoHouse() {
    ClearHouse();
    if ($("#chkPhoto").attr("checked") == true) {
        page_index = 1;
        GetHouseData(communityID, '1', 'pudate', page_index, page_size);
    }
    else {
        GetHouseData(communityID, '0', 'pudate', page_index, page_size);
    }
}
function OrderBy(strType) {
    if (strType == "1") {
        $("#link_Price").addClass("orderdown");
        $("#link_SquareMeter").removeClass("orderdown");
        $("#link_PubDate").removeClass("orderdown");
        ClearHouse();


        if ($("#chkPhoto").attr("checked") == true) {
            page_index = 1;
            GetHouseData(communityID, '1', 'strPrice', page_index, page_size);
        }
        else {
            GetHouseData(communityID, '0', 'strPrice', page_index, page_size);
        }
    }
    if (strType == "2") {
        HouseType = "2";
        $("#link_Price").removeClass("orderdown");
        $("#link_SquareMeter").addClass("orderdown");
        $("#link_PubDate").removeClass("orderdown");
        ClearHouse();

        if ($("#chkPhoto").attr("checked") == true) {

            GetHouseData(communityID, '1', 'strSquareMeter', page_index, page_size);
        }
        else {
            GetHouseData(communityID, '0', 'strSquareMeter', page_index, page_size);
        }
    }
    if (strType == "3") {
        HouseType = "3";
        $("#link_Price").removeClass("orderdown");
        $("#link_SquareMeter").removeClass("orderdown");
        $("#link_PubDate").addClass("orderdown");
        ClearHouse();
        if ($("#chkPhoto").attr("checked") == true) {
            GetHouseData(communityID, '1', 'pudate', page_index, page_size);
        }
        else {
            GetHouseData(communityID, '0', 'pudate', page_index, page_size);
        }
    }
}


function ObtainCommunity() {

    var icon = new GIcon();
    icon.image = "../../Content/Image/marker_trans.png";
    icon.shadow = "../../Content/Image/marker_trans.png";
    icon.iconSize = new GSize(21, 35);
    icon.shadowSize = new GSize(37, 35);
    icon.iconAnchor = new GPoint(10, 35);
    icon.infoWindowAnchor = new GPoint(10, 3);

    map.clearOverlays();
    $.ajax(
        {
            url: "/Ajax/SerachMap/",
            datatype: "json",
            cache: false,
            data: { x1: x1, x2: x2, y1: y1, y2: y2, SearchTypeID: $("#hidSearchType").val(), ID: $("#hidID").val() },
            type: "post",
            success: function (o) {
                var t = eval('(' + o + ')'); //强制转换一下json字符串，生成json对象 

                if (t.Code == "A00005") {
                    $.each(t.Data, function (i, item) {

                        var lblNumID = "lblNumberID" + i;
                        var markerRight = "markerRight" + i;
                        var strHtmlContent = "<div onmouseover=\"onmouseOver(this.id,'" + markerRight + "','" + lblNumID + "')\" onclick=\"ShowWindow(this.id)\" id=\"" + this.ID + "\" onmouseout=\"onmouseOut(this.id,'" + markerRight + "','" + lblNumID + "')\" class=\"maskleft\">" + item.RealName + "<label class=\"lblHidden\" id='" + lblNumID + "'> " + item.NickName + " </label><div id='" + markerRight + "' class=\"maskright\"/></div>";
                        var point = new GLatLng(item.LocationLat, item.LocationLng);
                        var marker = CreateMarkers(point, icon, strHtmlContent);
                        setTimeout(function () { map.addOverlay(marker); }, i * 100);
                    });
                }
            },
            error: function (request) {

            }
        });


}

function CloseContainer() {
    $("#HouseContainer").addClass("styleContainer");
}

function ShowWindow(strCommunityID) {

    ClearHouse();
    var clickType = $("#hidSearchType").val();
    page_index = 1;
    $("#chkPhoto").attr("checked", false);
    communityID = strCommunityID;
    //    GetHouseData(clickType,strCommunityID, page_index, page_size);
    GetHouseData(clickType, strCommunityID);
    var strElementID = "#" + strCommunityID;
    var X = $(strElementID).offset().top;
    var Y = $(strElementID).offset().left;
    $("#HouseContainer").removeClass("styleContainer");
    $("#dvHouseWindPanel").css({ position: "absolute", display: "block", top: X / 2 + "px", left: Y / 2 + "px" });
}

function GetHouseData(clickType, strCommunityID) {

    $.ajax(
        {
            url: "/Ajax/ObtainMapUserInfo",
            datatype: "json",
            cache: false,
            data: { "clickType": clickType, "UID": strCommunityID },
            type: "post",
            success: function (data) {
                var t = eval('(' + data + ')');
                console.log(t.Data.CurrUser.NickName);
                $("#userPhoto").attr("src", t.Data.CurrUser.MidAvartar);
                $("#UserName").html(t.Data.CurrUser.RealName + "(" + t.Data.CurrUser.NickName + ")");
                $("#lblDesc").html(t.Data.CurrUser.Description);
                $("#aAddr").html(t.Data.CurrUser.provinceName + t.Data.CurrUser.cityName + t.Data.CurrUser.locationDetailsDesc);
                var url = "/" + t.Data.CurrUser.ID + "/";
                $("#UserName").attr("href", url);
                $("#map2_propwind_listlink").attr("href", url);
                $("#olContent").html("");
                var html = "";
                $.each(t.Data.Top3MiniBlogs, function (i, item) {

                    html += "<li style='background-color: white;' class=''> <a>" + (i + 1) + "." + item.OriginalContent + "&nbsp;</a> </li>";
                    $("#olContent").html(html);

                });
            }
        });
}



function IntMapEvent() {
    GEvent.addListener(map, 'movestart', function () { CloseContainer(); });
    GEvent.addListener(map, 'moveend', function () { RefreshMap(); });
}
function RefreshMap() {
    GetMapLngAndLat();
    ObtainCommunity();
}
function ClearHouse() {
    //$("#trOne").html("");
    // $("#trTwo").html("");
}

function CreateMarkers(point, ico, label) {
    return new LabeledMarker(point, { icon: ico, labelText: label });
}


function CreateMarker(point, icon, label) {
    var marker = new LabeledMarker(point, { icon: icon, labelText: label, labelOffset: new GSize(-6, -10) });
    GEvent.addListener(marker, 'click', function () {
        alert("经度为：" + point.lng() + "纬度为：" + point.lat());
    });
    return marker;
}





function onmouseOver(id, strRight, strLblID) {
    map.disableDragging();
    var elementLeft = "#" + id;
    var elementRight = "#" + strRight;
    var elementLbl = "#" + strLblID;
    $(elementLeft).removeClass("maskleft");
    $(elementLeft).addClass("left_hover");
    $(elementLeft).parent().css({ zIndex: "10001" });
    $(elementRight).removeClass("maskright");
    $(elementRight).addClass("right_hover");
    $(elementLbl).removeClass("lblHidden");
    $(elementLbl).addClass("lblShow");

}

function onmouseOut(id, strRight, strLblID) {
    map.enableDragging();
    var elementLeft = "#" + id;
    var elementRight = "#" + strRight;
    var elementLbl = "#" + strLblID;
    $(elementLeft).removeClass("left_hover");
    $(elementLeft).addClass("maskleft");
    $(elementLeft).parent().css({ zIndex: "-999" });
    $(elementRight).removeClass("right_hover");
    $(elementRight).addClass("maskright");
    $(elementLbl).removeClass("lblShow");
    $(elementLbl).addClass("lblHidden");
}

function IntNav() {
    totalPage = page_count / page_size;
    var rex = /^-?\d+$/;

    if (rex.test(totalPage) != true) {
        totalPage = Math.floor(page_count / page_size) + 1;
    }

    $("#recordinfo").text(page_index + "/" + totalPage);
}

function goPrevPage() {

    if (page_index != 1) {
        page_index = page_index - 1;
        GetWindowSource();
    }

}

function goNextPage() {
    if (page_index < totalPage) {
        page_index = page_index + 1;
        GetWindowSource();
    }
}

function GetWindowSource() {
    if (HouseType == "1") {
        ClearHouse();
        if ($("#chkPhoto").attr("checked") == true) {
            GetHouseData(communityID, '1', 'strPrice', page_index, page_size);
        }
        else {
            GetHouseData(communityID, '0', 'strPrice', page_index, page_size);
        }
    }
    if (HouseType == "2") {
        ClearHouse();
        if ($("#chkPhoto").attr("checked") == true) {
            GetHouseData(communityID, '1', 'strSquareMeter', page_index, page_size);
        }
        else {
            GetHouseData(communityID, '0', 'strSquareMeter', page_index, page_size);
        }
    }
    if (HouseType == "3") {
        ClearHouse();
        if ($("#chkPhoto").attr("checked") == true) {
            GetHouseData(communityID, '1', 'pudate', page_index, page_size);
        }
        else {
            GetHouseData(communityID, '0', 'pudate', page_index, page_size);
        }
    }
}


function getMapAreaList() {
    //$("#dllAreaList").FillOptions("/Service/ObtainHouseSource.html?strGetArea=GetAreaList", { datatype: "json", textfield: "AreaName", valuefiled: "LngAndLat" });
    //    $("#dllPrice").FillOptions("/Service/ObtainHouseSource.html?strPrice=strPrice", { datatype: "json", textfield: "PriceTitle", valuefiled: "PriceRange" });
    //    $("#dllAcr").FillOptions("/Service/ObtainHouseSource.html?strAcr=strAcr", { datatype: "json", textfield: "AcrTitle", valuefiled: "AcrRange" });
    //    $("#dllRoom").FillOptions("/Service/ObtainHouseSource.html?strRoom=strRoom", { datatype: "json", textfield: "RoomTitle", valuefiled: "RoomNum" });
    //    $("#dllPrice").AddOption("价格不限", "-1", true, 0);
    //    $("#dllAcr").AddOption("选择面积", "-1", true, 0);
    //    $("#dllRoom").AddOption("选择房型", "-1", true, 0);
    //    $("#dllAreaList").AddOption("选择区域", "-1", true, 0);
    //    $("#dllBlockList").AddOption("选择板块", "-1", true, 0);
}

function getBlockList(strEval) {

    var strLng = strEval.substring(0, strEval.lastIndexOf(","));
    var strLat = strEval.substring(strEval.lastIndexOf(",") + 1, strEval.lastIndexOf("|"));
    var strAreaID = strEval.substring(strEval.lastIndexOf("|") + 1);
    if (strLng == null || strLng == "") {
        return;
    }
    $("#dllBlockList").FillOptions("/Service/ObtainHouseSource.html?strAreaID=" + strAreaID, { datatype: "json", textfield: "BlockName", valuefiled: "LngAndLat" });
    $("#dllBlockList").AddOption("选择板块", "-1", true, 0);
    var point = new GLatLng(strLat, strLng);
    map.panTo(point);
}

function setNewPoint(strEval) {
    var strLng = strEval.substring(0, strEval.lastIndexOf(","));
    var strLat = strEval.substring(strEval.lastIndexOf(",") + 1);
    var point = new GLatLng(strLat, strLng);
    map.panTo(point);
}

function setPrice(strEval) {
    var strFrom = strEval.substring(0, strEval.lastIndexOf(","));
    var strEnd = strEval.substring(strEval.lastIndexOf(",") + 1);
    priceFrom = strFrom;
    priceEnd = strEnd;
    ObtainCommunity();
}

function SetRoomNum(strEval) {

    roomNum = strEval;
    ObtainCommunity();
}

function SetAcr(strEval) {
    var strFrom = strEval.substring(0, strEval.lastIndexOf(","));
    var strEnd = strEval.substring(strEval.lastIndexOf(",") + 1);
    acrFrom = strFrom;
    acrEnd = strEnd;
    ObtainCommunity();
}

function SetPoint() {
    var strName = "" + $("#txtHouseText").val();
    var geocoder;
    geocoder = new GClientGeocoder();
    geocoder.getLocations(strName, findGlatLng);
}


function findGlatLng(response) {
    if (response || response.Status.code == 200) {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
        map.panTo(point);
        map.addOverlay(new GMarker(point));
    }
}